
# Create your views here.
from rest_framework import status
from rest_framework.generics import ListCreateAPIView, CreateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView,View
from Admin.utils import token_confirm

from Admin.adminSerializers import LoginSerializer,AdminSerializer, UserSerializer,ArticlesSerializer,DisGroupSerializer,QuestionsSerializer

from Admin.models import Admin_User,Articles, Questions,\
     Discussion_groups, User


"""
在这个里面要干什么？
对管理员的管理
展示、管理管理员的相关信息

对用户进行增删改查操作
展示所有的用户信息
展示某位用户的所有文章

对文章进行增删改查操作
展示所有的文章信息
展示某篇文章的所有评论详情

对讨论组进行增删改查操作
展示所有的讨论组

对问题进行增删改查操作
管理问题、回答
展示所有的问题
展示问题的回答

"""


#登录管理
class YuYunLoginView(CreateAPIView):
    """
        管理员登录验证接口
    """
    queryset = Admin_User.objects.all()
    serializer_class = LoginSerializer

    def create(self,request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data) # 反向序列化
        if serializer.is_valid():
            print("验证成功")
            # print(serializer.data)
            #1.生成token
            admin_name = serializer.data.get('admin_name')
            # print(admin_name)
            administrator = Admin_User.objects.filter(admin_name=admin_name).first()
            # print(administrator)
            admin_id = administrator.admin_id
            # print(administrator.admin_password)
            token = token_confirm.generate_validate_token(uid=admin_id)
            # print(token)
            serializer2 = AdminSerializer(instance=administrator)
            # print(serializer2.data)
            #2.返回数据
            return Response({
                'code':status.HTTP_200_OK,
                'msg':'登录成功',
                'data':{
                    'adminInfo':serializer2.data,
                    'authToken':token
                }
            })
        else:
            #验证失败
            print("验证失败")
            return Response({
                'code':1004,
                'msg':"校验参数错误",
                'errors':serializer.errors,
                'token':None
            })

#管理员
class YuYunAdminView(ListCreateAPIView):
    """
        返回所有管理员信息
    """
    queryset = Admin_User.objects.all()
    serializer_class = AdminSerializer

    # def get(self,request):
    #     print('项目运行成功')
    #     return Response({
    #         'code': 200,
    #         'msg': '项目运行成功'
    #     })

    #查询所有的用户信息，然后返回
    def list(self, request, *args, **kwargs):
        #获取查询集
        queryset = self.get_queryset()
        #做序列化
        serializer = self.get_serializer(queryset, many=True)
        # print(serializer.data)
        return Response({
            'code': 200,
            'msg': '获取管理员列表成功',
            'data': serializer.data
        })

#用户
class YuYunUserView(ListCreateAPIView):
    """
        返回所有的用户信息
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        return Response({
            'code': 200,
            'msg': 'ok',
            'data':serializer.data
        })

#文章
class YuYunArticleView(ListCreateAPIView):
    """
        返回所有的文章信息
    """
    queryset = Articles.objects.all()
    serializer_class = ArticlesSerializer

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)

        return Response({
            'code': 200,
            'msg': 'ok',
            'data': serializer.data
        })

#问题
class YuYunQuestionView(ListCreateAPIView):
    """
        返回所有问题相关的信息
    """
    queryset = Questions.objects.all()
    serializer_class = QuestionsSerializer

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        return Response({
            'code': 200,
            'msg': 'ok',
            'data': serializer.data
        })

#讨论组
class YuYunDisGroupView(ListCreateAPIView):
    """
        返回所有讨论组相关的信息
    """
    queryset = Discussion_groups.objects.all()
    serializer_class = DisGroupSerializer  #序列化器

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        return Response({
            'code': 200,
            'msg': 'ok',
            'data': serializer.data
        })


